import 'package:auto_size_text_plus/auto_size_text_plus.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/gen/assets.gen.dart';
import 'package:sgy_flutter/page/detail/logic.dart';
import 'package:sgy_flutter/page/detail/widget/ble_state_view.dart';

// 信号,电量,功率偏移,固件版本
class CommonInfoView extends GetView<DetailLogic> {
  const CommonInfoView({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
              bottom: BorderSide(color: Get.theme.primaryColor, width: 6))),
      padding: EdgeInsets.symmetric(vertical: 16, horizontal: 16),
      child: BleStateView2(
          rightBleManager: controller.rightBleManager,
          bleManager: controller.bleManager,
          builder: (left, right) {
            bool isDoubleSlide = left && right;
            return Row(
              children: [
                Expanded(child: iconLabel(isDoubleSlide)),
                Expanded(child: buildValue(isDoubleSlide, left ? controller.bleManager : controller.rightBleManager, "L")),
                Visibility(visible: right && isDoubleSlide, child: Expanded(child: buildValue(isDoubleSlide, isDoubleSlide ? controller.rightBleManager: null, "R"))),
              ],
            );
            return Table(
              columnWidths: {
                1: FlexColumnWidth(),
                2: FlexColumnWidth(),
              },
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              children: _buildTableRows(left ? controller.bleManager : null,
                  right ? controller.rightBleManager : null),
            );
          }),
    );
  }

  final double rowHeight = 40;

  Widget buildValue(bool isDoubleSlide, BleManager? manager, String title) {
    printInfo(info: "buildValue:${manager?.runtimeType}");
    return Column(
      children: [
        Container(
          height: isDoubleSlide ? 20 : 0,
          alignment: Alignment.center,
          child: isDoubleSlide
              ? Text(
                  "$title",
                  style: TextStyle(fontSize: 15, color: Color(0xff9c9c9c)),
                )
              : null,
        ),
        Container(
          height: rowHeight,
          alignment: Alignment.center,
          child: Obx(() => Text(manager?.rssi.value != null
              ? '${manager?.rssi.value} dBm'
              : "-")),
        ),
        Container(
          height: rowHeight,
          alignment: Alignment.center,
          child: Obx(() => Text(manager?.battery.value != null
              ? '${manager?.battery.value}%'
              : "-")),
        ),
        Container(
          height: rowHeight,
          alignment: Alignment.center,
          child: Obx(() => Text(manager?.powerOffset.value != null
              ? calcPowerOffset(manager?.powerOffset.value)
              : "-")),
        ),
        Container(
          height: rowHeight,
          alignment: Alignment.center,
          child: Obx(() => AutoSizeText(manager?.deviceInfo.value != null
              ? '${manager?.deviceInfo.value?.firmwareRevision}'
              : "-")),
        )
      ],
    );
  }

  Widget iconLabel(bool isDoubleSlide) {
    return Column(
      children: [
        Container(
          height: isDoubleSlide ? 20 : 0,
        ),
        Container(
          height: rowHeight,
          alignment: Alignment.centerLeft,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                Assets.image.iconXhqd.path,
                width: 20,
              ).marginOnly(right: 8),
              Expanded(
                child: AutoSizeText(
                  "信号强度".tr,
                  style: TextStyle(fontSize: 15, color: Color(0xff1e1e1e)),
                  maxLines: 1,
                ),
              ),
            ],
          ),
        ),
        Container(
          height: rowHeight,
          child: Row(
            children: [
              Image.asset(
                Assets.image.iconBattery.path,
                width: 20,
              ).marginOnly(right: 8),
              Expanded(
                child: AutoSizeText(
                  "电池电量".tr,
                  style: TextStyle(fontSize: 15, color: Color(0xff1e1e1e)),
                  maxLines: 1,
                ),
              ),
            ],
          ),
        ),
        Container(
          height: rowHeight,
          child: Row(
            children: [
              Image.asset(
                Assets.image.iconGlpy.path,
                width: 20,
              ).marginOnly(right: 8),
              Expanded(
                child: AutoSizeText(
                  "功率偏移".tr,
                  style: TextStyle(fontSize: 15, color: Color(0xff1e1e1e)),
                  maxLines: 1,
                ),
              ),
            ],
          ),
        ),
        Container(
          height: rowHeight,
          child: Row(
            children: [
              Image.asset(
                Assets.image.iconVersion.path,
                width: 20,
              ).marginOnly(right: 8),
              Expanded(
                child: AutoSizeText(
                  "固件版本".tr,
                  style: TextStyle(fontSize: 15, color: Color(0xff1e1e1e)),
                  maxLines: 1,
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  List<TableRow> _buildTableRows(BleManager? manager1, BleManager? manager2) {
    bool isDoubleSlide = manager1 != null && manager2 != null;
    List<TableRow> list = List.empty(growable: true);
    if (isDoubleSlide) {
      list.add(TableRow(
        children: [
          Container(),
          Align(
            alignment: Alignment.center,
            child: Text(
              "L",
              style: TextStyle(fontSize: 15, color: Color(0xff9c9c9c)),
            ),
          ),
          Visibility(
            visible: true,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                "R",
                style: TextStyle(fontSize: 15, color: Color(0xff9c9c9c)),
              ),
            ),
          ),
        ],
      ));
    }
    final manager = manager1 ?? manager2;
    final managerOther = manager2 ?? manager1;
    list.addAll([
      _buildTableRow(
          imagePath: Assets.image.iconXhqd.path,
          label: "信号强度".tr,
          lWidget: Obx(() => Text(manager?.rssi.value != null
              ? '${manager?.rssi.value} dBm'
              : "-")),
          rWidget: isDoubleSlide
              ? Obx(() => Text(managerOther?.rssi.value != null
                  ? '${managerOther?.rssi.value} dBm'
                  : "-"))
              : null),
      _buildTableRow(
          imagePath: Assets.image.iconBattery.path,
          label: "电池电量".tr,
          lWidget: Obx(() => Text(manager?.battery.value != null
              ? '${manager?.battery.value}%'
              : "-")),
          rWidget: isDoubleSlide
              ? Obx(() => Text(managerOther?.battery.value != null
                  ? '${managerOther?.battery.value}%'
                  : "-"))
              : null),
      _buildTableRow(
          imagePath: Assets.image.iconGlpy.path,
          label: "功率偏移".tr,
          lWidget: Obx(() => Text(manager?.powerOffset.value != null
              ? calcPowerOffset(manager?.powerOffset.value)
              : "-")),
          rWidget: isDoubleSlide
              ? Obx(() => Text(managerOther?.powerOffset.value != null
                  ? calcPowerOffset(managerOther?.powerOffset.value)
                  : "-"))
              : null),
      _buildTableRow(
          imagePath: Assets.image.iconVersion.path,
          label: "固件版本".tr,
          lWidget: Obx(() => AutoSizeText(manager?.deviceInfo.value != null
              ? '${manager?.deviceInfo.value?.firmwareRevision}'
              : "-")),
          rWidget: isDoubleSlide
              ? Obx(() => AutoSizeText(managerOther?.deviceInfo.value != null
                  ? '${managerOther?.deviceInfo.value?.firmwareRevision}'
                  : "-"))
              : null)
    ]);
    return list;
  }

  String calcPowerOffset(int? powerOffset) {

    final powerOffsetValue = (powerOffset ?? 0) / 10 - 100;
    if (powerOffsetValue >= 0) {
      return "${powerOffsetValue}%";
    } else {
      return "${powerOffsetValue}%";
    }
  }

  TableRow _buildTableRow(
      {required String imagePath,
      required String label,
      String? l,
      Widget? rWidget,
      Widget? lWidget,
      String? r}) {
    List<Widget> children = List.empty(growable: true);

    children.addAll([
      Row(
        children: [
          Image.asset(
            imagePath,
            width: 25,
          ),
          AutoSizeText(
            label,
            style: TextStyle(fontSize: 15, color: Color(0xff1e1e1e)),
            maxLines: 1,
          ).marginOnly(left: 12),
        ],
      ).paddingSymmetric(vertical: 6),
      Align(
        alignment: Alignment.center,
        child: DefaultTextStyle(
          style: TextStyle(fontSize: 15, color: Color(0xff9c9c9c)),
          child: lWidget ?? AutoSizeText("$l"),
        ).paddingSymmetric(vertical: 6),
      ),
    ]);
    if (r != null || rWidget != null) {
      children.add(Align(
        alignment: Alignment.center,
        child: DefaultTextStyle(
          style: TextStyle(fontSize: 15, color: Color(0xff9c9c9c)),
          child: rWidget ?? AutoSizeText("$r"),
        ),
      ));
    }
    return TableRow(children: children);
  }
}
